<?php

declare(strict_types=1);

namespace App\CustHub\Listener;

use App\CustHub\Event\ContactStats;
use Carbon\Carbon;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerInterface;

#[Listener]
class ContactStatsListener implements ListenerInterface
{

    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            ContactStats::class,
        ];
    }

    public function process(object $event): void
    {
        $redis = $this->container->get(Redis::class);

        $date = Carbon::now()->toDateString();

        if ($event->type == 0) {
            //新增
            $redis->sAdd("user_stats_add:{$date}-{$event->robotId}", $event->userId);
        } elseif ($event->type == 1) {
            //流失
            $redis->sAdd("user_stats_remove:{$date}-{$event->robotId}", $event->userId);
        }
    }

}